/**
 * 购物车维护
 * @author yuan_rz
 * @date 2019/01/15 15:00
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        element,
        vueData = {

        },
        table_where = {

        },
        cols = [[
              {type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'ROW_ID', width: 60, title: '序号', fixed: 'left',hide : true}
            , {field: 'GWCQDOCNO', width: 180, title: '单据编号', fixed: 'left', sort: true}
            , {field: 'GWCQDOCDATE', width: 100, title: '单据日期', sort: true, search: 'default',searchOpts: {type: 'daterange'}}
            , {field: 'GWCQ010', width: 80, title: '经销商编号',search: 'default',searchOpts: {icon: true}}
            , {field: 'GWCQ010_NAME', width: 150, title: '经销商名称'}
            , {field: 'GWCQ112', width: 150, title: '客户'}
            , {field: 'GWCQ050', width: 150, title: '备注'}
            , {field: 'GWCQACTI', width: 80, title: '单据状态',filterDatas: app.datas_('base_gwai700_gwcqacti'),search: 'default',searchOpts: {type: 'select'}}
            /*, {field: 'GWCQ101', width: 80, title: '是否转单',hide : true}*/
            /*, {
                width: 60,
                title: '',
                toolbar: '#script-gwai700setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }*/
        ]],
        gwai700SettingAction,
        moduleName = '购物车'
    ;

    var gwai700 = app.Module.extend({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwai700',
                _searchs: [{
                    id: 'gwai700', tableId: 'gwai700_table', datas: _searchsArr.pushArray([
                        {field: 'GWCQ001', width: 80, title: '产品编号',search: 'default',searchOpts: {icon: true}},
                        {field: 'GWCQ109', width: 150, title: '活动编号',search: 'default',searchOpts: {icon: true}}
                    ])
                }],
                _loadTplsFull: [{
                    dom: 'gwai700_edit', tpl: 'base/gwai700_edit'
                },{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },
        initTable: function () {
            var sort = {"field":"GWCQDOCNO","sort":"desc"};
            table_where['_SORT'] = JSON.stringify(sort);
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwai700_table'),
                    url: g_ctxPath + 'base/gwai700/getListData',
                    exportsUrl: g_ctxPath + 'base/gwai700/exportListData',
                    exportsName: moduleName + '数据',
                    title: moduleName + '表',
                    id: 'gwai700_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: false,
                    page: true,
                    where: table_where,
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwai700setting-action').innerHTML).render({}, function (string) {
                    gwai700SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwai700_table)', function (obj) {

                    layer.tips(gwai700SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            tipsIndex = index;
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '1':
                                        self._openTplFull({
                                            tag: 'gwai700_edit',
                                            tagData: {
                                                obj : obj,
                                                moduleName : moduleName
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: moduleName + '编辑'
                                            }
                                        });
                                        break;
                                    case '2':
                                        app.confirm({
                                            msg: '是否删除选择的数据？',
                                            ok: function () {
                                                self.deleteData(obj);
                                            }
                                        });
                                        break;
                                }
                                layer.close(index);
                                tipsIndex = null;
                            });
                        }
                    });
                    return false;
                });

                //监听行双击事件
                table.on('rowDouble(gwai700_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwai700_edit',
                        moduleName : moduleName,
                        tagData: {
                            is_check: true,
                            obj : obj,
                            moduleName : moduleName,
                            is_show: true
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '编辑'
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwai700_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwai700_edit',
                                tagData: {
                                    is_add : true,
                                    moduleName : moduleName
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '编辑'
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.deleteDataBatch();
                                }
                            });
                            break;
                        case 'transformOrder':
                            self.transformOrder();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },
        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai700_table', where: table_where});
        },
        deleteData: function (obj) {
            app.ajax({
                xurl: 'base/gwai700/deleteData',
                xdata: obj.data,
                loading: true,
                okmsg: moduleName +'信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });
        },
        deleteDataBatch: function(){
            self.getCheckData(function(_xdata){
                for(var i=0;i<_xdata.length;i++){
                    if(_xdata[i].GWCQACTI == 'Z'){
                        app.showWarning('已转单数据不允许删除!');
                        return;
                    }
                }
                app.ajax({
                    xurl: 'base/gwai700/deleteDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: moduleName + '信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            });
        },
        transformOrder : function(){
            self.getCheckData(function(_xdata){
                for(var i=0;i<_xdata.length;i++){
                    if(_xdata[i].GWCQACTI == 'Z'){
                        app.showWarning('不能重复转单!');
                        return;
                    }
                }
                app.ajax({
                    xurl: 'base/gwai700/transformOrder',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: moduleName + '转订单成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            });
        },
        getCheckData: function(fun){
            var checkStatus = table.checkStatus('gwai700_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('base_gwai700');
                    _temp.GWCQENT = v.GWCQENT;
                    _temp.GWCQDOCNO = v.GWCQDOCNO;
                    _temp.GWCQDOCDATE = v.GWCQDOCDATE;
                    _temp.GWCQ010 = v.GWCQ010;
                    _temp.GWCQ101 = v.GWCQ101;
                    _temp.GWCQACTI = v.GWCQACTI;
                    _temp.GWCQ109 = v.GWCQ109;
                    _xdata.push(_temp);
                });
                fun(_xdata);
            }
        },
        _ready: function () {
            vueData.serchData = table_where;
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    searchId: 'gwai700',
                    methods: {
                        handleIconClick: function(field){
                            var vue = this;
                            if(field == 'GWCQ001'){//产品开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwam001',
                                            columnList: [
                                                {label: '设备编号', prop: 'GWAM001', width: '150'},
                                                {label: '说明', prop: 'GWAML003', width: '150'},
                                                {label: '助记码', prop: 'GWAML004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCQ001 = data.GWAM001
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择产品',
                                        area: ['600px', '465px']
                                    }
                                });
                            }else if(field == 'GWCQ010'){//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config:{
                                            method:'q_gwak001',
                                            columnList:[
                                                {label:'客户编号',prop:'GWAK001', width: 150},
                                                {label:'客户简称',prop:'GWAKL003', width: 150},
                                                {label:'客户全称',prop:'GWAKL004', width: 150}
                                            ]
                                        },
                                        cb:function (data) {
                                            vue.isearch.fields.GWCQ010 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area:['600px', '470px']
                                    }
                                });
                            }else if(field == 'GWCQ109'){//活动开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            columnList: [{label: '活动编号', prop: 'GWDC001', width: '150'},
                                                {label: '活动说明', prop: 'GWDC003', width: '150'},
                                                {label: '日期', prop: 'GWDC008', width: '150'},
                                                {label: '状态码', prop: 'GWDCACTI_NAME', width: '150'}
                                            ],
                                            url: 'crm/gwdc003/getGwdc001Data'
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWCQ109 = data.GWDC001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择活动编号',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    },
                    watch:{
                        'isearch.fields.GWCQDOCDATE':function(newValue,oldValue){
                            if(newValue != null && newValue.length == 2){
                                this.isearch.fields.GWCQDOCDATE_START = newValue[0];
                                this.isearch.fields.GWCQDOCDATE_END = newValue[1];
                            }else{
                                this.isearch.fields.GWCQDOCDATE_START = ''
                                this.isearch.fields.GWCQDOCDATE_END = ''
                            }
                        }
                    },
                    created: function(){
                    }
                }]
            });

            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new gwai700();

});
